import os

from langchain.sql_database import SQLDatabase
from langchain_community.llms.tongyi import Tongyi

os.environ["DASHSCOPE_API_KEY"] = "sk-10920ad0a9d542af96353edd7ab3e613"

# 连接到数据库：
db_user = "root"
db_password = "root"
db_host = "127.0.0.1"
db_name = "langchain_demo"
db = SQLDatabase.from_uri(f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}")

from langchain.chains import create_sql_query_chain
# from langchain_openai import ChatOpenAI

# llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
llm = Tongyi()
chain = create_sql_query_chain(llm, db)
response = chain.invoke({"question": "How many students are there"})
print(response)


# 原始sql，想用人话代替
result_sql = db.run(response)
print(result_sql)

"""11"""